<?php
# 文件名称:online.model.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
class onlineModel extends C_model {
	var $_lists   = array();
	
	var $_pagination = null;
	
    function onlineModel()
    {
        parent::C_model();
    }
	//可用表单
	function getForms()
	{
		$SYS =&get_instance();
		$tmp[] = C_html::_('select.option', '0', '- '.C_text::_('SelectType').' '.C_text::_('SelectList').' -', 'id', 'name');
		//权限
		$currentUser = & $SYS->session->get('admin_name');
		if(!in_array("1",explode(",",$currentUser->get('gid'))))
		{
			$where = ' AND a.created_by='.$currentUser->get('id');
		}else $where='';
		//数据集
		$query = 'SELECT a.id,b.fieldvalue name'.
				' FROM '.$this->_db->_table_prefix.'fdform'.' AS a '.
				' ,'.$this->_db->_table_prefix.'fields'.' AS b '.
				' ,'.$this->_db->_table_prefix.'language'.' AS c '.
				' WHERE c.backend=1 AND c.lang_type=1'.
				' AND b.lang=c.id AND b.module=1 AND b.fieldname="name"'.
				' AND a.id=b.recordid '.
				$where;
		
		$tmp = array_merge($tmp, $this->_getObjectList( $query));		
		return $tmp;
	}
	//单个在线交流记录集
	function getItem()
	{
		if (!empty( $this->_one )) 
		{
			return $this->_one;
		}
		$SYS =&get_instance();
		//参数过滤
		$id = $SYS->request->getVar( 'cid', array(0), '', 'array' );
		//数据
		$query = 'SELECT *'.
				' FROM '.$this->_db->_table_prefix.'online'.' AS a '.
				' WHERE a.id='.$id[0]
				;
		$this->_db->setQuery($query);
		$this->_one = $this->_db->loadAssoc();
		
		$query = 'SELECT b.id,b.fieldvalue,c.`default`,c.id langid'.
				' FROM '.$this->_db->_table_prefix.'fields'.' AS b '.
				' ,'.$this->_db->_table_prefix.'language'.' AS c '.
				' WHERE b.recordid='.$id[0].
				' AND b.module=2'.
				' AND b.lang=c.id'.
				' ORDER BY c.`backend` desc,langid'
		;
		
		$this->_db->setQuery($query);
		$this->_itemfield = $this->_db->loadAssocList();
		return $this->_one;
	}
	//多语言字段
	function getItemfield()
	{
		return $this->_itemfield;
	}
	//在线交流记录集
	function getItems()
	{
		if (!empty( $this->_data ))
		{
			return $this->_data;
		}
		$SYS =&get_instance();
		// 获取请求的数据
		$filter_order		= $SYS->request->getVar( "filter_order",		'a.no_order',		'post',	'cmd' );
		$filter_order_Dir	= $SYS->request->getVar( "filter_order_Dir",	'asc',	'post',			'word' );
		$search				= $SYS->request->getVar( "search",			'', 			'post',			'string' );
		$limit = $SYS->request->getInt( 'limit',10 );
		$limitstart = $SYS->request->getInt( 'limitstart' );
		//过滤查询
		$where = array();
		if (isset( $search ) && $search!= '')
		{
			$searchEscaped = $this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false );
			$tmp = '(b.fieldvalue LIKE '.$searchEscaped.' OR a.qq LIKE '.$searchEscaped.
			' OR a.msn LIKE '.$searchEscaped.' OR a.taobao LIKE '.$searchEscaped.' OR a.alibaba LIKE '.$searchEscaped.
			' OR a.skype LIKE '.$searchEscaped.
			' OR d.fieldvalue LIKE '.$searchEscaped;			
			$tmp .= ')';
			$where[] = $tmp;
		}
		
		//语言列
		$where[]='a.id=b.recordid';
		$where[]='b.module=2';
		$where[]='b.fieldname="name"';
		$defaultlang=$SYS->lang->getDefault(1,'backend');
		$where[]='b.lang='.$defaultlang['id'];
		
		$where[]='c.id=a.form';
		$where[]='c.id=d.recordid';
		$where[]='d.module=1';
		$where[]='d.fieldname="name"';
		$defaultlang=$SYS->lang->getDefault(1,'backend');
		$where[]='d.lang='.$defaultlang['id'];
		//权限
		$currentUser = $SYS->session->get('admin_name');
		if(!in_array("1",explode(",",$currentUser->get('gid'))))
		{
			$query = 'SELECT id'.
				' FROM '.$this->_db->_table_prefix.'fdform'.' AS a '.
				' WHERE a.created_by='.$currentUser->get('id');
			$tmpformid=$this->_getArrayList($query);
			$tmpid=array();
			for($i=0;$i<count($tmpformid);$i++)
			{
				$tmpid[]= $tmpformid[$i]['id'];				
			}
			$where[] = 'a.form IN ('.implode(",", $tmpid).')';
		}
		
		$orderby = ' ORDER BY '. $filter_order .' '. $filter_order_Dir;
		$where = (count($where) ? ' WHERE '.implode(' AND ', $where) : '');
		//分页
		_import('html/pagination',false);
		$query = 'SELECT a.*,b.fieldvalue,d.fieldvalue form' .
				' FROM '.$this->_db->_table_prefix.'online'.' AS a '.
				' ,'.$this->_db->_table_prefix.'fields'.' AS b '.
				' ,'.$this->_db->_table_prefix.'fdform'.' AS c '.
				' ,'.$this->_db->_table_prefix.'fields'.' AS d '.
				$where.
				$orderby;
		$this->_db->setQuery($query);
		$total = $this->_db->loadResult();
		$this->_pagination = new C_pagination( $total, $limitstart, $limit );
		//数据集
		$query = 'SELECT a.*,b.fieldvalue,d.fieldvalue form' .
				' FROM '.$this->_db->_table_prefix.'online'.' AS a '.
				' ,'.$this->_db->_table_prefix.'fields'.' AS b '.
				' ,'.$this->_db->_table_prefix.'fdform'.' AS c '.
				' ,'.$this->_db->_table_prefix.'fields'.' AS d '.
				$where.
				$orderby;
		$this->_data = $this->_getArrayList( $query, $this->_pagination->limitstart, $this->_pagination->limit );
		$this->_lists['order_Dir']	= $filter_order_Dir;
		$this->_lists['order']		= $filter_order;
		$this->_lists['search']		= $search;
		return $this->_data;
	}
	//排序
	function getLists()
	{		
		return $this->_lists;
	}
	//分页
	function getPagination()
	{
		return $this->_pagination;
	}
	
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>